import 'package:flutter/services.dart';
import '../../manger/user_manger.dart';
import 'package:package_info_plus/package_info_plus.dart';


class QYSDK {
  static const MethodChannel _channel = MethodChannel('com.example.qysdk');

  // 调用 showQYSession 并传递数据
  static Future<void> showQYSession() async {
    final user = UserInfos.shared.userModel;
    final userId = UserInfos.shared.userId;
    var phone = "";
    var email = "";

    if (UserInfos.shared.token.isNotEmpty && user != null) {
      final phoneValue = user.mobile?.mobile;
      final emailValue = user.email?.email;

      if (phoneValue != null && phoneValue.isNotEmpty) {
        phone = phoneValue;
      } 
      
      if (emailValue != null && emailValue.isNotEmpty) {
        email = emailValue;
      } 
    } 

    try {
      final packageInfo = await PackageInfo.fromPlatform();
      final version = packageInfo.version;

      await _channel.invokeMethod('showQYSession', {
        'userId': userId,
        'phone': phone,
        'email': email,
        'version': version,
      });
    } on PlatformException catch (e) {
      print("Error calling showQYSession: ${e.message}");
    }
  }
}
